#ifndef _SCALERANGEDLG_H
#define _SCALERANGEDLG_H

//#include <string>
#include "lib/colorbutton.h"
class CScaleRangeDlg : public CDialogImpl<CScaleRangeDlg>, 
                 public CWinDataExchange<CScaleRangeDlg>
{
	
public:
    enum { IDD = IDD_SCALEDLG };
 
    BEGIN_MSG_MAP(CScaleRangeDlg)
        MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
        MESSAGE_HANDLER(WM_CLOSE, OnClose)
		MESSAGE_HANDLER(WM_SHOWWINDOW, OnShow)
		COMMAND_ID_HANDLER(IDOK, OnOK)
        COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		COMMAND_HANDLER(IDC_RADIO2, BN_CLICKED, OnBnClickedRadio2)
		COMMAND_HANDLER(IDC_RADIO_RANGE, BN_CLICKED, OnBnClickedRadio2)

		REFLECT_NOTIFICATIONS ()
	END_MSG_MAP()
 
    LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        CenterWindow();

		edit_max = GetDlgItem(IDC_EDIT_MAX);
		edit_min = GetDlgItem(IDC_EDIT_MIN);
		UpdateDlgUI();
		DoDataExchange(FALSE);
        return TRUE;    // let the system set the focus

    }

	LRESULT OnShow(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
		return 0;
	}


    LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        EndDialog(IDCANCEL);
        return 0;
    }

	LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
    {
        EndDialog(wID);
        return 0;
    }
	LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
    {
		DoDataExchange(TRUE);
		//COLORREF bg_color = cb_bgcolor.GetColor();
		TCHAR buf[256];
		edit_max.GetWindowText(buf, 256);
		d_max = _tstof( buf);
		edit_min.GetWindowText(buf, 256);
		d_min = _tstof( buf);
		if(d_max<d_min){
			MessageBox(_T("Error: max. is less than min."));
			return 1;
		}
        EndDialog(wID);
        return 0;
    }

	BEGIN_DDX_MAP(CScaleRangeDlg)
        DDX_RADIO(IDC_RADIO_RANGE, r_range)
		DDX_RADIO(IDC_RADIO_UNIT, r_unit)

		//DDX_CONTROL(IDC_COMBOCOLOR, cb_color);
    END_DDX_MAP()

protected:
	int r_unit;
	int r_range;
	CEdit edit_max, edit_min;

	double d_max, d_min;

private:
	void UpdateDlgUI(){
		if(r_range == 1){
			edit_max.EnableWindow(TRUE);
			edit_min.EnableWindow(TRUE);
			
		}else{
			edit_max.EnableWindow(FALSE);
			edit_min.EnableWindow(FALSE);
		}
		TCHAR buf[256];
		_stprintf( buf, _T("%g"), d_max);
		edit_max.SetWindowText( buf );
		_stprintf( buf, _T("%g"), d_min);
		edit_min.SetWindowText( buf );
	}
public:
	LRESULT OnBnClickedRadio2(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/){
		DoDataExchange(TRUE);
		UpdateDlgUI();
		return 1;
	}

	void SetRangeType(int r){r_range = r;}
	int  GetRangeType() {return r_range;}

	void SetRange(double min, double max){
		d_max = max;
		d_min = min;
	}

	void GetRange(double &min, double &max){
		min = d_min;
		max = d_max;
	}

	void SetUnit(int t){
		r_unit = t;
	}

	int GetUnit(){return r_unit;}
};
#endif